﻿/***************************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 *  
 * *************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Xml;
using System.Data.SqlClient;

namespace StockDataGeter
{
    /// <summary>
    /// 股票数据获取界面
    /// </summary>
    public partial class FrmGetGuideLines : Form
    {
        WebBrowser webb1;
        HtmlDocument htmldoc1;
        System.Threading.Thread th;

        //-----------------当前获取条件--------------------------------
        string beginStockNO;
        string endStockNO;
        int beginYear;
        int beginQuarter;
        int endYear;
        int endQuarter;
        string reportType;
        int stockType = 1;
        //------------------------------------------------------------------

        public FrmGetGuideLines()
        {
            InitializeComponent();
            //关闭线程检查
            Control.CheckForIllegalCrossThreadCalls = false;
            webb1 = new WebBrowser();
            webb1.Navigate("about:blank");
            htmldoc1 = webb1.Document.OpenNew(true);
        }       

        #region event handlers

        private void btnStopGetting_Click(object sender, EventArgs e)
        {
            th.Abort();
            this.txtBeginQuarter.Enabled = true;
            this.txtBeginYear.Enabled = true;
            this.txtEndYear.Enabled = true;
            this.txtEndQuarter.Enabled = true;
            this.txtBeginStock.Enabled = true;
            this.txtEndStock.Enabled = true;
            this.cbReportType.Enabled = true;
            this.btnStartGetting.Enabled = true;
            this.btnStopGetting.Enabled = false;
        }

        private void btnStartGetting_Click(object sender, EventArgs e)
        {
            this.txtBeginQuarter.Enabled = false;
            this.txtBeginYear.Enabled = false;
            this.txtEndYear.Enabled = false;
            this.txtEndQuarter.Enabled = false;
            this.txtBeginStock.Enabled = false;
            this.txtEndStock.Enabled = false;
            this.cbReportType.Enabled = false;
            this.btnStartGetting.Enabled = false;
            this.btnStopGetting.Enabled = true;

            beginStockNO = this.txtBeginStock.Text.Trim();
            endStockNO = this.txtEndStock.Text.Trim();
            try
            {
                beginYear = int.Parse(this.txtBeginYear.Text.Trim());
                beginQuarter = int.Parse(this.txtBeginQuarter.Text.Trim());
                endYear = int.Parse(this.txtEndYear.Text.Trim());
                endQuarter = int.Parse(this.txtEndQuarter.Text.Trim());
            }
            catch (System.FormatException)
            {
                MessageBox.Show("请输入正确的年份和季度");
                return;
            }

            reportType = (string)this.cbReportType.SelectedItem;
           
            if (reportType.IndexOf("金融") == -1) { stockType = 2; }

            if (reportType.Equals("资产负债表"))
            {
                th = new System.Threading.Thread(new System.Threading.ThreadStart(getData1));
                th.Start();
            }
            else if (reportType.Equals("利润分配表"))
            {
                th = new System.Threading.Thread(new System.Threading.ThreadStart(getData2));
                th.Start();
            }
            else if (reportType.Equals("现金流量表"))
            {
                th = new System.Threading.Thread(new System.Threading.ThreadStart(getData3));
                th.Start();
            }
            else if (reportType.Equals("金融－资产负债表"))
            {
                th = new System.Threading.Thread(new System.Threading.ThreadStart(getData1_1));
                th.Start();
            }
            else if (reportType.Equals("金融－利润分配表"))
            {
                th = new System.Threading.Thread(new System.Threading.ThreadStart(getData1_2));
                th.Start();
            }
            else if (reportType.Equals("金融－现金流量表"))
            {
                th = new System.Threading.Thread(new System.Threading.ThreadStart(getData1_3));
                th.Start();
            }
            else if (reportType.Equals("成长性"))
            {
                th = new System.Threading.Thread(new System.Threading.ThreadStart(getData4to8));
                th.Start();
            }
            else if (reportType.Equals("财务速览"))
            {
                th = new System.Threading.Thread(new System.Threading.ThreadStart(getData9));
                th.Start();
            }
            else if (reportType.Equals("网易主要财务指标"))
            {
                th = new System.Threading.Thread(new System.Threading.ThreadStart(getData10));
                th.Start();
            }
            
        }

        #endregion

       

        //private void button1_Click(object sender, EventArgs e)
        //{
        //    th = new System.Threading.Thread(new System.Threading.ThreadStart(getData1));
        //    th.Start();
            //WebBrowser webb1 = new WebBrowser();
            //webb1.Navigate("about:blank");
            //HtmlDocument htmldoc1 = webb1.Document.OpenNew(true);

            //int start1 = strWebContent.IndexOf("<table width=\"98%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\" xmlns:ms=\"urn:schemas-microsoft-com:xslt\">");
            //start1 = strWebContent.IndexOf("<table width=\"98%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\" xmlns:ms=\"urn:schemas-microsoft-com:xslt\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //int end1 = strWebContent.IndexOf("</table>", start1);
            //string content1 = strWebContent.Substring(start1, end1 - start1 + 8);
            //htmldoc1.Write(content1);

            //string str1 = htmldoc1.GetElementsByTagName("TR")[int.Parse(this.textBox1.Text)].InnerText;

            //MessageBox.Show(str1.ToString());
        //}

        private void getData2()
        {
            List<Dictionary<int, int>> guideLines = new List<Dictionary<int, int>>();

            Dictionary<int, int> guideLines1 = new Dictionary<int, int>();
            guideLines1.Add(0, 2001);
            guideLines1.Add(1, 2002);
            guideLines1.Add(2, 2003);
            guideLines1.Add(3, 2004);
            guideLines1.Add(4, 2005);
            guideLines.Add(guideLines1);

            Dictionary<int, int> guideLines2 = new Dictionary<int, int>();
            guideLines2.Add(0, 2006);
            guideLines2.Add(1, 2007);
            guideLines2.Add(2, 2008);
            guideLines2.Add(3, 2009);
            guideLines2.Add(4, 2010);
            guideLines2.Add(5, 2011);
            guideLines2.Add(6, 2012);
            guideLines.Add(guideLines2);

            Dictionary<int, int> guideLines3 = new Dictionary<int, int>();
            guideLines3.Add(0, 2013);
            guideLines3.Add(1, 2014);
            guideLines3.Add(2, 2015);
            guideLines3.Add(3, 2016);
            guideLines3.Add(4, 2017);
            guideLines3.Add(5, 2018);
            guideLines.Add(guideLines3);

            Dictionary<int, int> guideLines4 = new Dictionary<int, int>();
            guideLines4.Add(0, 2019);
            guideLines4.Add(1, 2020);
            guideLines4.Add(2, 2021);
            guideLines.Add(guideLines4);

            Dictionary<int, int> guideLines5 = new Dictionary<int, int>();
            guideLines5.Add(0, 2022);
            guideLines5.Add(1, 2023);
            guideLines5.Add(2, 2024);
            guideLines5.Add(3, 2025);
            guideLines5.Add(4, 2026);
            guideLines5.Add(5, 2027);
            guideLines5.Add(6, 2028);
            guideLines5.Add(7, 2029);
            guideLines5.Add(8, 2030);
            guideLines5.Add(9, 2031);
            guideLines5.Add(10, 2032);
            guideLines5.Add(11, 2033);
            guideLines5.Add(12, 2034);
            guideLines5.Add(13, 2035);
            guideLines5.Add(14, 2036);
            guideLines.Add(guideLines5);

            List<List<Dictionary<int, int>>> gls = new List<List<Dictionary<int, int>>>();
            gls.Add(guideLines);

            GetGuideLines(gls, "http://stockdata.stock.hexun.com/finance_report2_{1}_{0}.shtml", new string[] { "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">" }, beginStockNO, endStockNO, beginYear, beginQuarter, endYear, endQuarter, stockType);
        }

        private void getData3()
        {
            List<Dictionary<int, int>> guideLines = new List<Dictionary<int, int>>();

            Dictionary<int, int> guideLines1 = new Dictionary<int, int>();
            guideLines1.Add(0, 3001);
            guideLines1.Add(1, 3002);
            guideLines1.Add(2, 3003);
            guideLines1.Add(3, 3004);
            guideLines1.Add(4, 3005);
            guideLines1.Add(5, 3006);
            guideLines1.Add(6, 3007);
            guideLines1.Add(7, 3008);
            guideLines1.Add(8, 3009);
            guideLines1.Add(9, 3010);
            guideLines1.Add(10, 3011);
            guideLines1.Add(11, 3012);
            guideLines1.Add(12, 3013);
            guideLines1.Add(13, 3014);
            guideLines1.Add(14, 3015);
            guideLines1.Add(15, 3016);
            guideLines.Add(guideLines1);

            Dictionary<int, int> guideLines2 = new Dictionary<int, int>();
            guideLines2.Add(0, 3017);
            guideLines2.Add(1, 3018);
            guideLines2.Add(2, 3019);
            guideLines2.Add(3, 3020);
            guideLines2.Add(4, 3021);
            guideLines2.Add(5, 3022);
            guideLines2.Add(6, 3023);
            guideLines2.Add(7, 3024);
            guideLines2.Add(8, 3025);
            guideLines2.Add(9, 3026);
            guideLines2.Add(10, 3027);
            guideLines2.Add(11, 3028);
            guideLines2.Add(12, 3029);
            guideLines2.Add(13, 3030);
            guideLines2.Add(14, 3031);
            guideLines2.Add(15, 3032);
            guideLines2.Add(16, 3033);
            guideLines2.Add(17, 3034);
            guideLines2.Add(18, 3035);
            guideLines2.Add(19, 3036);
            guideLines2.Add(20, 3037);
            guideLines2.Add(21, 3038);
            guideLines2.Add(22, 3039);
            guideLines2.Add(23, 3040);
            guideLines2.Add(24, 3041);
            guideLines2.Add(25, 3042);
            guideLines2.Add(26, 3043);
            guideLines2.Add(27, 3044);
            guideLines2.Add(28, 3045);
            guideLines2.Add(29, 3046);
            guideLines2.Add(30, 3047);
            guideLines2.Add(31, 3048);
            guideLines2.Add(32, 3049);
            guideLines2.Add(33, 3050);
            guideLines2.Add(34, 3051);
            guideLines2.Add(35, 3052);
            guideLines2.Add(36, 3053);
            guideLines2.Add(37, 3054);
            guideLines2.Add(38, 3055);
            guideLines2.Add(39, 3056);
            guideLines2.Add(40, 3057);
            guideLines2.Add(41, 3058);
            guideLines2.Add(42, 3059);
            guideLines2.Add(43, 3060);
            guideLines2.Add(44, 3061);
            guideLines2.Add(45, 3062);
            guideLines2.Add(46, 3063);
            guideLines2.Add(47, 3064);
            guideLines2.Add(48, 3065);
            guideLines2.Add(49, 3066);
            guideLines2.Add(50, 3067);
            guideLines2.Add(51, 3068);
            guideLines2.Add(52, 3069);
            guideLines2.Add(53, 3070);
            guideLines2.Add(54, 3071);
            guideLines2.Add(55, 3072);
            guideLines2.Add(56, 3073);
            guideLines2.Add(57, 3074);
            guideLines2.Add(58, 3075);
            guideLines2.Add(59, 3076);
            guideLines2.Add(60, 3077);
            guideLines.Add(guideLines2);

            List<List<Dictionary<int, int>>> gls = new List<List<Dictionary<int, int>>>();
            gls.Add(guideLines);

            GetGuideLines(gls, "http://stockdata.stock.hexun.com/finance_report3_{1}_{0}.shtml",  new string[] {"<table width=\"98%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\" xmlns:ms=\"urn:schemas-microsoft-com:xslt\">" }, beginStockNO, endStockNO, beginYear, beginQuarter, endYear, endQuarter, stockType);
        }

        private void getData1()
        {
            List<Dictionary<int, int>> guideLines = new List<Dictionary<int, int>>();

            Dictionary<int, int> guideLines1 = new Dictionary<int, int>();
            guideLines1.Add(0, 1);
            guideLines1.Add(1, 2);
            guideLines1.Add(2, 3);
            guideLines1.Add(3, 4);
            guideLines1.Add(4, 5);
            guideLines1.Add(5, 6);
            guideLines1.Add(6, 7);
            guideLines1.Add(7, 8);
            guideLines1.Add(8, 9);
            guideLines1.Add(9, 10);
            guideLines1.Add(10, 11);
            guideLines1.Add(11, 12);
            guideLines1.Add(12, 13);
            guideLines1.Add(13, 14);
            guideLines1.Add(14, 15);
            guideLines1.Add(15, 16);
            guideLines1.Add(16, 17);
            guideLines1.Add(17, 18);
            guideLines1.Add(18, 19);
            guideLines1.Add(19, 20);
            guideLines1.Add(20, 21);
            guideLines1.Add(21, 22);
            guideLines1.Add(22, 23);
            guideLines1.Add(23, 24);
            guideLines.Add(guideLines1);

            Dictionary<int, int> guideLines2 = new Dictionary<int, int>();
            guideLines2.Add(0, 25);
            guideLines2.Add(1, 26);
            guideLines2.Add(2, 27);
            guideLines2.Add(3, 28);
            guideLines2.Add(4, 29);
            guideLines2.Add(5, 30);
            guideLines.Add(guideLines2);

            Dictionary<int, int> guideLines3 = new Dictionary<int, int>();
            guideLines3.Add(0, 31);
            guideLines3.Add(1, 32);
            guideLines3.Add(2, 33);
            guideLines3.Add(3, 34);
            guideLines3.Add(4, 35);
            guideLines3.Add(5, 36);
            guideLines3.Add(6, 37);
            guideLines3.Add(7, 38);
            guideLines.Add(guideLines3);

            Dictionary<int, int> guideLines4 = new Dictionary<int, int>();
            guideLines4.Add(0, 39);
            guideLines4.Add(1, 40);
            guideLines4.Add(2, 41);
            guideLines4.Add(3, 42);
            guideLines4.Add(4, 43);
            guideLines4.Add(5, 44);
            guideLines4.Add(6, 45);
            guideLines.Add(guideLines4);

            Dictionary<int, int> guideLines5 = new Dictionary<int, int>();
            guideLines5.Add(0, 46);
            guideLines.Add(guideLines5);

            Dictionary<int, int> guideLines6 = new Dictionary<int, int>();
            guideLines6.Add(0, 47);
            guideLines6.Add(1, 48);
            guideLines6.Add(2, 49);
            guideLines6.Add(3, 50);
            guideLines6.Add(4, 51);
            guideLines6.Add(5, 52);
            guideLines6.Add(6, 53);
            guideLines6.Add(7, 54);
            guideLines6.Add(8, 55);
            guideLines6.Add(9, 56);
            guideLines6.Add(10, 57);
            guideLines6.Add(11, 58);
            guideLines6.Add(12, 59);
            guideLines6.Add(13, 60);
            guideLines6.Add(14, 61);
            guideLines6.Add(15, 62);
            guideLines6.Add(16, 63);
            guideLines6.Add(17, 64);
            guideLines.Add(guideLines6);

            Dictionary<int, int> guideLines7 = new Dictionary<int, int>();
            guideLines7.Add(0, 65);
            guideLines7.Add(1, 66);
            guideLines7.Add(2, 67);
            guideLines7.Add(3, 68);
            guideLines7.Add(4, 69);
            guideLines7.Add(5, 70);
            guideLines.Add(guideLines7);

            Dictionary<int, int> guideLines8 = new Dictionary<int, int>();
            guideLines8.Add(0, 71);
            guideLines8.Add(1, 72);
            guideLines8.Add(2, 73);
            guideLines.Add(guideLines8);

            Dictionary<int, int> guideLines9 = new Dictionary<int, int>();
            guideLines9.Add(0, 74);
            guideLines9.Add(1, 75);
            guideLines9.Add(2, 76);
            guideLines9.Add(3, 77);
            guideLines9.Add(4, 78);
            guideLines9.Add(5, 79);
            guideLines9.Add(6, 80);
            guideLines9.Add(7, 81);
            guideLines9.Add(8, 82);
            guideLines9.Add(9, 83);
            guideLines.Add(guideLines9);

            Dictionary<int, int> guideLines10 = new Dictionary<int, int>();
            guideLines10.Add(0, 84);
            guideLines.Add(guideLines10);

            List<List<Dictionary<int, int>>> gls = new List<List<Dictionary<int, int>>>();
            gls.Add(guideLines);

            GetGuideLines(gls, "http://stockdata.stock.hexun.com/finance_report1_{1}_{0}.shtml", new string[] { "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">" }, beginStockNO, endStockNO, beginYear, beginQuarter, endYear, endQuarter, stockType);
        }

        private void getData1_1()
        {
            //int start1 = strWebContent.IndexOf("<TABLE width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">");
            //start1 = strWebContent.IndexOf("<TABLE width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE width=\"98%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE width=\"98%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE width=\"98%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE width=\"98%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE width=\"98%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", start1 + 10);


            List<Dictionary<int, int>> gls1 = new List<Dictionary<int, int>>();

            List<Dictionary<int, int>> gls2 = new List<Dictionary<int, int>>();

            Dictionary<int, int> guideLines1 = new Dictionary<int, int>();
            guideLines1.Add(0, 85);
            guideLines1.Add(1, 86);
            guideLines1.Add(2, 87);
            guideLines1.Add(3, 88);
            guideLines1.Add(4, 89);
            guideLines1.Add(5, 90);
            guideLines1.Add(6, 91);
            guideLines1.Add(7, 92);
            guideLines1.Add(8, 93);
            guideLines1.Add(9, 94);
            guideLines1.Add(10, 95);
            guideLines1.Add(11, 8);
            guideLines1.Add(12, 9);
            guideLines1.Add(13, 96);
            guideLines1.Add(14, 11);
            guideLines1.Add(15, 97);
            guideLines1.Add(16, 98);
            guideLines1.Add(17, 2);
            guideLines1.Add(18, 99);
            guideLines1.Add(19, 100);
            guideLines1.Add(20, 101);
            guideLines1.Add(21, 102);
            guideLines1.Add(22, 103);
            guideLines1.Add(23, 104);
            guideLines1.Add(24, 105);
            guideLines1.Add(25, 106);
            guideLines1.Add(26, 107);
            guideLines1.Add(27, 108);
            guideLines1.Add(28, 109);
            guideLines1.Add(29, 7);
            guideLines1.Add(30, 110);
            guideLines1.Add(31, 15);
            guideLines1.Add(32, 16);
            guideLines1.Add(33, 18);
            guideLines1.Add(34, 20);
            guideLines1.Add(35, 111);
            guideLines1.Add(36, 19);
            guideLines1.Add(37, 112);
            guideLines1.Add(38, 113);
            guideLines1.Add(39, 24);
            gls1.Add(guideLines1);

            Dictionary<int, int> guideLines2 = new Dictionary<int, int>();
            guideLines2.Add(0, 114);
            guideLines2.Add(1, 115);
            guideLines2.Add(2, 116);
            guideLines2.Add(3, 117);
            guideLines2.Add(4, 118);
            guideLines2.Add(5, 119);
            guideLines2.Add(6, 120);
            guideLines2.Add(7, 121);
            guideLines2.Add(8, 122);
            guideLines2.Add(9, 123);
            guideLines2.Add(10, 124);
            guideLines2.Add(11, 25);
            guideLines2.Add(12, 27);
            guideLines2.Add(13, 125);
            guideLines2.Add(14, 28);
            guideLines2.Add(15, 126);
            guideLines2.Add(16, 30);
            gls1.Add(guideLines2);

            Dictionary<int, int> guideLines3 = new Dictionary<int, int>();
            guideLines3.Add(0, 127);
            guideLines3.Add(1, 128);
            guideLines3.Add(2, 129);
            guideLines3.Add(3, 33);
            guideLines3.Add(4, 35);
            guideLines3.Add(5, 36);
            guideLines3.Add(6, 37);
            guideLines3.Add(7, 38);
            gls1.Add(guideLines3);

            Dictionary<int, int> guideLines4 = new Dictionary<int, int>();
            guideLines4.Add(0, 39);
            guideLines4.Add(1, 42);
            guideLines4.Add(2, 40);
            guideLines4.Add(3, 41);
            guideLines4.Add(4, 130);
            guideLines4.Add(5, 131);
            gls1.Add(guideLines4);

            Dictionary<int, int> guideLines5 = new Dictionary<int, int>();
            guideLines5.Add(0, 132);
            guideLines5.Add(1, 133);
            gls1.Add(guideLines5);

            Dictionary<int, int> guideLines6 = new Dictionary<int, int>();
            guideLines6.Add(0, 46);
            gls1.Add(guideLines6);

            Dictionary<int, int> guideLines7 = new Dictionary<int, int>();
            guideLines7.Add(0, 134);
            guideLines7.Add(1, 135);
            guideLines7.Add(2, 136);
            guideLines7.Add(3, 137);
            guideLines7.Add(4, 138);
            guideLines7.Add(5, 139);
            guideLines7.Add(6, 140);
            guideLines7.Add(7, 141);
            guideLines7.Add(8, 142);
            guideLines7.Add(9, 143);
            guideLines7.Add(10, 144);
            guideLines7.Add(11, 145);
            guideLines7.Add(12, 146);
            guideLines7.Add(13, 147);
            guideLines7.Add(14, 148);
            guideLines7.Add(15, 149);
            guideLines7.Add(16, 47);
            guideLines7.Add(17, 150);
            guideLines7.Add(18, 48);
            guideLines7.Add(19, 50);
            guideLines7.Add(20, 57);
            guideLines7.Add(21, 52);
            guideLines7.Add(22, 53);
            guideLines7.Add(23, 151);
            guideLines7.Add(24, 152);
            guideLines7.Add(25, 153);
            guideLines7.Add(26, 58);
            guideLines7.Add(27, 154);
            guideLines7.Add(28, 155);
            guideLines7.Add(29, 156);
            guideLines7.Add(30, 64);
            gls2.Add(guideLines7);

            Dictionary<int, int> guideLines8 = new Dictionary<int, int>();
            guideLines8.Add(0, 157);
            guideLines8.Add(1, 158);
            guideLines8.Add(2, 159);
            guideLines8.Add(3, 160);
            guideLines8.Add(4, 161);
            guideLines8.Add(5, 162);
            guideLines8.Add(6, 65);
            guideLines8.Add(7, 67);
            guideLines8.Add(8, 69);
            guideLines8.Add(9, 68);
            guideLines8.Add(10, 70);
            gls2.Add(guideLines8);

            Dictionary<int, int> guideLines9 = new Dictionary<int, int>();
            guideLines9.Add(0, 71);
            guideLines9.Add(1, 163);
            guideLines9.Add(2, 73);
            gls2.Add(guideLines9);

            Dictionary<int, int> guideLines10 = new Dictionary<int, int>();
            guideLines10.Add(0, 74);
            guideLines10.Add(1, 75);
            guideLines10.Add(2, 164);
            guideLines10.Add(3, 76);
            guideLines10.Add(4, 77);
            guideLines10.Add(5, 165);
            guideLines10.Add(6, 81);
            guideLines10.Add(7, 166);
            guideLines10.Add(8, 83);
            gls2.Add(guideLines10);

            Dictionary<int, int> guideLines11 = new Dictionary<int, int>();
            guideLines11.Add(0, 84);
            gls2.Add(guideLines11);

            List<List<Dictionary<int, int>>> gls = new List<List<Dictionary<int,int>>>();
            gls.Add(gls1);
            gls.Add(gls2);

            GetGuideLines(gls, "http://stockdata.stock.hexun.com/finance_report1_{2}_{0}-{1}.shtml", new string[] { "<TABLE width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">", "<TABLE width=\"98%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolorlight=\"#CCCCCC\" bordercolordark=\"#ffffff\">"}, beginStockNO, endStockNO, beginYear, beginQuarter, endYear, endQuarter, stockType);
        }


        private void getData1_2()
        {
            //int start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"100%\">");
            //start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"100%\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"100%\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"100%\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"100%\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"100%\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"100%\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"100%\">", start1 + 10);

            List<Dictionary<int, int>> guideLines = new List<Dictionary<int, int>>();

            Dictionary<int, int> guideLines1 = new Dictionary<int, int>();
            guideLines1.Add(0, 2001);
            guideLines1.Add(1, 2037);
            guideLines1.Add(2, 2038);
            guideLines1.Add(3, 2039);
            guideLines1.Add(4, 2040);
            guideLines1.Add(5, 2041);
            guideLines1.Add(6, 2042);
            guideLines1.Add(7, 2043);
            guideLines1.Add(8, 2044);
            guideLines1.Add(9, 2045);
            guideLines1.Add(10, 2046);
            guideLines1.Add(11, 2047);
            guideLines1.Add(12, 2048);
            guideLines1.Add(13, 2049);
            guideLines1.Add(14, 2050);
            guideLines1.Add(15, 2051);
            guideLines1.Add(16, 2052);
            guideLines1.Add(17, 2009);
            guideLines1.Add(18, 2053);
            guideLines1.Add(19, 2054);
            guideLines1.Add(20, 2055);
            guideLines1.Add(21, 2056);
            guideLines1.Add(22, 2057);
            guideLines1.Add(23, 2058);
            guideLines1.Add(24, 2059);
            guideLines.Add(guideLines1);

            Dictionary<int, int> guideLines2 = new Dictionary<int, int>();
            guideLines2.Add(0, 2006);
            guideLines2.Add(1, 2007);
            guideLines2.Add(2, 2008);
            guideLines2.Add(3, 2010);
            guideLines2.Add(4, 2011);
            guideLines.Add(guideLines2);

            Dictionary<int, int> guideLines3 = new Dictionary<int, int>();
            guideLines3.Add(0, 2013);
            guideLines3.Add(1, 2014);
            guideLines3.Add(2, 2015);
            guideLines3.Add(3, 2016);
            guideLines3.Add(4, 2017);
            guideLines.Add(guideLines3);

            Dictionary<int, int> guideLines4 = new Dictionary<int, int>();
            guideLines4.Add(0, 2019);
            guideLines4.Add(1, 2020);
            guideLines4.Add(2, 2060);
            guideLines.Add(guideLines4);

            Dictionary<int, int> guideLines5 = new Dictionary<int, int>();
            guideLines5.Add(0, 2022);
            guideLines5.Add(1, 2023);
            guideLines5.Add(2, 2061);
            guideLines5.Add(3, 2062);
            guideLines5.Add(4, 2063);
            guideLines5.Add(5, 2064);
            guideLines.Add(guideLines5);

            Dictionary<int, int> guideLines6 = new Dictionary<int, int>();
            guideLines6.Add(0, 2065);
            guideLines6.Add(1, 2066);
            guideLines6.Add(2, 2028);
            guideLines.Add(guideLines6);

            Dictionary<int, int> guideLines7 = new Dictionary<int, int>();
            guideLines7.Add(0, 2031);
            guideLines7.Add(1, 2032);
            guideLines7.Add(2, 2067);
            guideLines7.Add(3, 2034);
            guideLines7.Add(4, 2035);
            guideLines.Add(guideLines7);

            Dictionary<int, int> guideLines8 = new Dictionary<int, int>();
            guideLines8.Add(0, 2036);
            guideLines.Add(guideLines8);

            List<List<Dictionary<int, int>>> gls = new List<List<Dictionary<int, int>>>();
            gls.Add(guideLines);

            GetGuideLines(gls, "http://stockdata.stock.hexun.com/finance_report2_{2}_{0}-{1}.shtml", new string[] { "<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"100%\">" }, beginStockNO, endStockNO, beginYear, beginQuarter, endYear, endQuarter, stockType);
        }

        private void getData1_3()
        {

            //int start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"98%\" xmlns:ms=\"urn:schemas-microsoft-com:xslt\">");
            //start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"98%\" xmlns:ms=\"urn:schemas-microsoft-com:xslt\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"98%\" xmlns:ms=\"urn:schemas-microsoft-com:xslt\">", start1 + 10);
            //start1 = strWebContent.IndexOf("<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"98%\" xmlns:ms=\"urn:schemas-microsoft-com:xslt\">", start1 + 10);


            List<Dictionary<int, int>> guideLines = new List<Dictionary<int, int>>();

            Dictionary<int, int> guideLines1 = new Dictionary<int, int>();
            guideLines1.Add(0, 3001);
            guideLines1.Add(1, 3078);
            guideLines1.Add(2, 3079);
            guideLines1.Add(3, 3080);
            guideLines1.Add(4, 3081);
            guideLines1.Add(5, 3082);
            guideLines1.Add(6, 3083);
            guideLines1.Add(7, 3084);
            guideLines1.Add(8, 3085);
            guideLines1.Add(9, 3086);
            guideLines1.Add(10, 3087);
            guideLines1.Add(11, 3088);
            guideLines1.Add(12, 3089);
            guideLines1.Add(13, 3090);
            guideLines1.Add(14, 3091);
            guideLines1.Add(15, 3092);
            guideLines1.Add(16, 3093);
            guideLines1.Add(17, 3094);
            guideLines1.Add(18, 3095);
            guideLines1.Add(19, 3096);
            guideLines1.Add(20, 3097);
            guideLines1.Add(21, 3098);
            guideLines1.Add(22, 3099);
            guideLines1.Add(23, 3100);
            guideLines1.Add(24, 3101);
            guideLines1.Add(25, 3102);
            guideLines1.Add(26, 3103);
            guideLines1.Add(27, 3104);
            guideLines1.Add(28, 3105);
            guideLines1.Add(29, 3106);
            guideLines1.Add(30, 3107);
            guideLines1.Add(31, 3108);
            guideLines1.Add(32, 3109);
            guideLines1.Add(33, 3110);
            guideLines1.Add(34, 3111);
            guideLines1.Add(35, 3112);
            guideLines1.Add(36, 3113);
            guideLines1.Add(37, 3114);
            guideLines1.Add(38, 3115);
            guideLines1.Add(39, 3116);
            guideLines1.Add(40, 3117);
            guideLines1.Add(41, 3118);
            guideLines1.Add(42, 3007);
            guideLines1.Add(43, 3119);
            guideLines1.Add(44, 3120);
            guideLines1.Add(45, 3121);
            guideLines1.Add(46, 3122);
            guideLines1.Add(47, 3123);
            guideLines1.Add(48, 3124);
            guideLines1.Add(49, 3125);
            guideLines1.Add(50, 3126);
            guideLines1.Add(51, 3127);
            guideLines1.Add(52, 3128);
            guideLines1.Add(53, 3129);
            guideLines1.Add(54, 3130);
            guideLines1.Add(55, 3131);
            guideLines1.Add(56, 3132);
            guideLines1.Add(57, 3133);
            guideLines1.Add(58, 3134);
            guideLines1.Add(59, 3135);
            guideLines1.Add(60, 3009);
            guideLines1.Add(61, 3136);
            guideLines1.Add(62, 3137);
            guideLines1.Add(63, 3138);
            guideLines1.Add(64, 3139);
            guideLines1.Add(65, 3140);
            guideLines1.Add(66, 3141);
            guideLines1.Add(67, 3142);
            guideLines1.Add(68, 3143);
            guideLines1.Add(69, 3144);
            guideLines1.Add(70, 3145);
            guideLines1.Add(71, 3146);
            guideLines1.Add(72, 3010);
            guideLines1.Add(73, 3011);
            guideLines1.Add(74, 3147);
            guideLines1.Add(75, 3148);
            guideLines1.Add(76, 3149);
            guideLines1.Add(77, 3150);
            guideLines1.Add(78, 3151);
            guideLines1.Add(79, 3014);
            guideLines1.Add(80, 3015);
            guideLines1.Add(81, 3072);
            guideLines.Add(guideLines1);

            Dictionary<int, int> guideLines2 = new Dictionary<int, int>();
            guideLines2.Add(0, 3017);
            guideLines2.Add(1, 3018);
            guideLines2.Add(2, 3019);
            guideLines2.Add(3, 3152);
            guideLines2.Add(4, 3153);
            guideLines2.Add(5, 3022);
            guideLines2.Add(6, 3154);
            guideLines2.Add(7, 3155);
            guideLines2.Add(8, 3025);
            guideLines2.Add(9, 3026);
            guideLines2.Add(10, 3027);
            guideLines2.Add(11, 3156);
            guideLines2.Add(12, 3157);
            guideLines.Add(guideLines2);

            Dictionary<int, int> guideLines3 = new Dictionary<int, int>();
            guideLines3.Add(0, 3030);
            guideLines3.Add(1, 3031);
            guideLines3.Add(2, 3158);
            guideLines3.Add(3, 3034);
            guideLines3.Add(4, 3159);
            guideLines3.Add(5, 3160);
            guideLines3.Add(6, 3037);
            guideLines3.Add(7, 3038);
            guideLines3.Add(8, 3161);
            guideLines3.Add(9, 3041);
            guideLines3.Add(10, 3042);
            guideLines3.Add(11, 3043);
            guideLines3.Add(12, 3045);
            guideLines3.Add(13, 3162);
            guideLines3.Add(14, 3047);
            guideLines.Add(guideLines3);

            Dictionary<int, int> guideLines4 = new Dictionary<int, int>();
            guideLines4.Add(0, 3194);
            guideLines4.Add(1, 3048);
            guideLines.Add(guideLines4);

            Dictionary<int, int> guideLines5 = new Dictionary<int, int>();
            guideLines5.Add(0, 3049);
            guideLines5.Add(1, 3051);
            guideLines5.Add(2, 3050);
            guideLines5.Add(3, 3052);
            guideLines5.Add(4, 3053);
            guideLines5.Add(5, 3054);
            guideLines5.Add(6, 3163);
            guideLines5.Add(7, 3055);
            guideLines5.Add(8, 3164);
            guideLines5.Add(9, 3165);
            guideLines5.Add(10, 3166);
            guideLines5.Add(11, 3167);
            guideLines5.Add(12, 3058);
            guideLines5.Add(13, 3059);
            guideLines5.Add(14, 3168);
            guideLines5.Add(15, 3169);
            guideLines5.Add(16, 3062);
            guideLines5.Add(17, 3063);
            guideLines5.Add(18, 3170);
            guideLines5.Add(19, 3171);
            guideLines5.Add(20, 3172);
            guideLines5.Add(21, 3173);
            guideLines5.Add(22, 3174);
            guideLines5.Add(23, 3175);
            guideLines5.Add(24, 3176);
            guideLines5.Add(25, 3177);
            guideLines5.Add(26, 3178);
            guideLines5.Add(27, 3179);
            guideLines5.Add(28, 3180);
            guideLines5.Add(29, 3181);
            guideLines5.Add(30, 3182);
            guideLines5.Add(31, 3183);
            guideLines5.Add(32, 3184);
            guideLines5.Add(33, 3185);
            guideLines5.Add(34, 3186);
            guideLines5.Add(35, 3187);
            guideLines5.Add(36, 3188);
            guideLines5.Add(37, 3189);
            guideLines5.Add(38, 3190);
            guideLines5.Add(39, 3016);
            guideLines5.Add(40, 3073);
            guideLines5.Add(41, 3191);
            guideLines5.Add(42, 3192);
            guideLines5.Add(43, 3193);
            guideLines5.Add(44, 3077);
            guideLines.Add(guideLines5);

            List<List<Dictionary<int, int>>> gls = new List<List<Dictionary<int, int>>>();
            gls.Add(guideLines);

            GetGuideLines(gls, "http://stockdata.stock.hexun.com/finance_report3_{2}_{0}-{1}.shtml", new string[] { "<TABLE border=\"1\" borderColorDark=\"#ffffff\" bordercolorlight=\"#CCCCCC\" cellPadding=\"0\" cellSpacing=\"0\" width=\"98%\" xmlns:ms=\"urn:schemas-microsoft-com:xslt\">" }, beginStockNO, endStockNO, beginYear, beginQuarter, endYear, endQuarter, stockType);
        }

        class FinancialCategoryInfo
        {
            public int CategoryNO;
            public int GuideLineCount;
            public string AddressTemplate;
        }

        private void getData10()
        {
            List<string> stockNOs = new List<string>();
            string[] stocks1 = DataTool.GetAllStocks(beginStockNO, endStockNO, 1);
            string[] stocks2 = DataTool.GetAllStocks(beginStockNO, endStockNO, 2);

            stockNOs.AddRange(stocks1);
            stockNOs.AddRange(stocks2);

            foreach (string stock in stockNOs)
            {
                string strWebContent = Tool.GetContentFromUrll(String.Format("http://quotes.money.163.com/corp/1029/unit=1,start=1996-12-31,end=2008-03-31,code={0},submit=%CC%E1+%BD%BB.html", stock));
                int start1 = 0;

                //if (start1 == -1)
                //{
                //    Log(String.Format("未找到股票{0}的主要财务指标数据", stock));
                //    continue;
                //}

                

                while (true)
                {
                    start1 = strWebContent.IndexOf("<table width=\"125\" border=\"0\" align=\"left\" cellpadding=\"5\" cellspacing=\"0\" class=\"dataTb04\">", start1 + 100, StringComparison.OrdinalIgnoreCase);

                    if (start1 == -1)
                        break;

                    int end1 = strWebContent.IndexOf("</table>", start1 + 100);
                    string content1 = strWebContent.Substring(start1, end1 - start1 + 8);
                    htmldoc1.OpenNew(true);
                    htmldoc1.Write(content1);

                    HtmlElementCollection trs = htmldoc1.GetElementsByTagName("TR");

                    DateTime dt = DateTime.Parse(trs[0].GetElementsByTagName("TD")[0].InnerText.Trim());

                    for (int i = 1; i <= 25; i++)
                    {
                        string str = trs[i].GetElementsByTagName("TD")[0].InnerText;
                        str = (str == null ? null : str.Trim().Replace("&nbsp;", ""));
                        decimal v = -77777.77m;
                        if (!string.IsNullOrEmpty(str))
                        {
                            try
                            {
                                v = decimal.Parse(str);
                            }
                            catch (System.FormatException)
                            {
                                Console.WriteLine("error string : " + str);
                            }
                        }

                        int quarter = 0;
                        if (dt.Month.Equals(3)) { quarter = 1; }
                        else if (dt.Month.Equals(6)) { quarter = 2; }
                        else if (dt.Month.Equals(9)) { quarter = 3; }
                        else if (dt.Month.Equals(12)) { quarter = 4; }

                        DataTool.InsertGuideLine(stock, 10000 + i, dt.Year, quarter, v);
                        
                    }
                }

                lbStatus.Items.Add(String.Format("stock : {0}", stock));
                lbStatus.SelectedIndex = lbStatus.Items.Count - 1;
            }
        }

        private void getData9()
        {       
            List<string> stockNOs = new List<string>();
            string[] stocks1 = DataTool.GetAllStocks(beginStockNO, endStockNO, 1);
            string[] stocks2 = DataTool.GetAllStocks(beginStockNO, endStockNO, 2);

            stockNOs.AddRange(stocks1);
            stockNOs.AddRange(stocks2);

            foreach (string stock in stockNOs)
            {               
                    string strWebContent = Tool.GetContentFromUrll(String.Format("http://stockdata.stock.hexun.com/finance_default_{0}.shtml", stock));
                    int start1 = strWebContent.IndexOf("<table width=\"603\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"#CDCED0\" style=\"margin-top:10px\">", StringComparison.OrdinalIgnoreCase);
                    if (start1 == -1)
                    {
                        Log(String.Format("未找到股票{0}的财务速览数据", stock));
                        continue;
                    }
                    int end1 = strWebContent.IndexOf("</table>", start1 + 1000);
                    string content1 = strWebContent.Substring(start1, end1 - start1 + 8);
                    htmldoc1.OpenNew(true);
                    htmldoc1.Write(content1);

                    HtmlElementCollection trs = htmldoc1.GetElementsByTagName("TR");

                    for (int i = 1; i < trs.Count; i++)
                    {
                        HtmlElementCollection tds = trs[i].GetElementsByTagName("TD");
                        string quarterString = tds[0].InnerText;

                        int year = int.Parse(quarterString.Substring(0, 2)) + 2000;
                        int quarter = 1;

                        if (quarterString.IndexOf("第1季") != -1)
                        {
                           quarter = 1;
                        }
                        else if (quarterString.IndexOf("中期") != -1)
                        {
                            quarter = 2;
                        }
                        else if (quarterString.IndexOf("前3季") != -1)
                        {
                            quarter = 3;
                        }
                        else if (quarterString.IndexOf("年度") != -1)
                        {
                            quarter = 4;
                        }

                        for (int j = 1; j < 10; j++)
                        {
                            string str = tds[j].InnerText;
                            str = (str == null ? null : str.Trim().Replace("%", ""));
                            decimal v = -77777.77m;
                            if (!string.IsNullOrEmpty(str))
                            {
                                try
                                {
                                    v = decimal.Parse(str);
                                }
                                catch (System.FormatException)
                                {
                                    Console.WriteLine("error string : " + str);
                                }
                            }
                         
                            DataTool.InsertGuideLine(stock, 9000 + j, year, quarter, v);
                            lbStatus.Items.Add(String.Format("stock : {0}", stock));
                            lbStatus.SelectedIndex = lbStatus.Items.Count - 1;
                        }
                    }
                
            }
        }

        

        private void getData4to8()
        {
            FinancialCategoryInfo info4 = new FinancialCategoryInfo();
            info4.CategoryNO = 4;
            info4.GuideLineCount = 5;
            info4.AddressTemplate = "http://stockdata.stock.hexun.com/finance_financeindex1_{0}.shtml";

            FinancialCategoryInfo info5 = new FinancialCategoryInfo();
            info5.CategoryNO = 5;
            info5.GuideLineCount = 5;
            info5.AddressTemplate = "http://stockdata.stock.hexun.com/finance_financeindex2_{0}.shtml";

            FinancialCategoryInfo info6 = new FinancialCategoryInfo();
            info6.CategoryNO = 6;
            info6.GuideLineCount = 4;
            info6.AddressTemplate = "http://stockdata.stock.hexun.com/finance_financeindex3_{0}.shtml";

            FinancialCategoryInfo info7 = new FinancialCategoryInfo();
            info7.CategoryNO = 7;
            info7.GuideLineCount = 5;
            info7.AddressTemplate = "http://stockdata.stock.hexun.com/finance_financeindex4_{0}.shtml";

            FinancialCategoryInfo info8 = new FinancialCategoryInfo();
            info8.CategoryNO = 8;
            info8.GuideLineCount = 3;
            info8.AddressTemplate = "http://stockdata.stock.hexun.com/finance_financeindex5_{0}.shtml";

            List<FinancialCategoryInfo> infos = new List<FinancialCategoryInfo>();
            infos.Add(info4);
            infos.Add(info5);
            infos.Add(info6);
            infos.Add(info7);
            infos.Add(info8);

            
            List<string> stockNOs = new List<string>();
            string[] stocks1 = DataTool.GetAllStocks(beginStockNO, endStockNO, 1);
            string[] stocks2 = DataTool.GetAllStocks(beginStockNO, endStockNO, 2);

            stockNOs.AddRange(stocks1);
            stockNOs.AddRange(stocks2);

            foreach (string stock in stockNOs)
            {
                foreach (FinancialCategoryInfo info in infos)
                {
                    string strWebContent = Tool.GetContentFromUrll(String.Format(info.AddressTemplate, stock));
                    int start1 = strWebContent.IndexOf("<table width=\"603\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" bgcolor=\"#cdced0\" style=\"MARGIN-TOP:15px\">", StringComparison.OrdinalIgnoreCase);
                    if (start1 == -1)
                    {
                        Log(String.Format("未找到股票{0}的{1}报表数据", stock, info.CategoryNO));
                        continue;
                    }
                    int end1 = strWebContent.IndexOf("</table>", start1 + 1000);
                    string content1 = strWebContent.Substring(start1, end1 - start1 + 8);
                    htmldoc1.OpenNew(true);
                    htmldoc1.Write(content1);

                    int flag = 2010;

                    for (int year = 2007; year > 1991; year--)
                    {
                        int i = flag - year;

                        HtmlElementCollection trs = htmldoc1.GetElementsByTagName("TR");

                        if (trs.Count <= i)
                        {
                            for (int j = 1; j < info.GuideLineCount + 1; j++)
                            {
                                DataTool.InsertGuideLine(stock, info.CategoryNO * 1000 + j, year, 4, -77777.77m);
                            }
                            continue;
                        }

                        if (!year.ToString().Substring(2, 2).Equals(trs[i].GetElementsByTagName("TD")[0].InnerText.Substring(0, 2)))
                        {
                            for (int j = 1; j < info.GuideLineCount + 1; j++)
                            {
                                DataTool.InsertGuideLine(stock, info.CategoryNO * 1000 + j, year, 4, -77777.77m);
                            }
                            flag -= 1;
                            continue;
                        }


                        for (int j = 1; j < info.GuideLineCount + 1; j++)
                        {
                            string str = trs[i].GetElementsByTagName("TD")[j].InnerText;
                            str = (str == null ? null : str.Trim().Replace("%", ""));
                            decimal v = -77777.77m;
                            if (!string.IsNullOrEmpty(str))
                            {
                                try
                                {
                                    v = decimal.Parse(str);
                                }
                                catch (System.FormatException)
                                {
                                    Console.WriteLine("error string : " + str);
                                }
                            }        

                            DataTool.InsertGuideLine(stock, info.CategoryNO * 1000 + j, year, 4, v);
                            lbStatus.Items.Add(String.Format("stock : {0} report : {1}", stock, info.CategoryNO));
                            lbStatus.SelectedIndex = lbStatus.Items.Count - 1;
                        }
                    }
                }
            }
        }

        

        private void GetGuideLines(List<List<Dictionary<int, int>>> guideLines, string address, string[] flags, string beginStock, string endStock, int beginYear, int beginQuarter, int endYear, int endQuarter, int StockType)
        {
            Dictionary<YearQuarter, string> quaters = new Dictionary<YearQuarter, string>();
            for (int i = 1992; i <= 1994; i ++)
            {
                quaters.Add(new YearQuarter(i, 4), i.ToString() + "-12-31");
            }

            for (int i = 1995; i <= 2001; i++)
            {                
                quaters.Add(new YearQuarter(i, 2), i.ToString() + "-06-3");
                quaters.Add(new YearQuarter(i, 4), i.ToString() + "-12-31");
            }

            for (int i = 2002; i <= 2007; i++)
            {                
                quaters.Add(new YearQuarter(i, 1), i.ToString() + "-03-15");
                quaters.Add(new YearQuarter(i, 2), i.ToString() + "-06-30");
                quaters.Add(new YearQuarter(i, 3), i.ToString() + "-09-30");
                quaters.Add(new YearQuarter(i, 4), i.ToString() + "-12-31");
            }

            quaters.Add(new YearQuarter(2008, 1), "2008-03-15");
            
            //string[] quarterStrings = new string[] { "03-15", "06-30", "09-30", "12-31" };

            string[] stocks = DataTool.GetAllStocks(beginStock, endStock, StockType);

            foreach (string stock in stocks)
            {
                foreach (KeyValuePair<YearQuarter, string> item in quaters)
                {

                    lbStatus.Items.Add(String.Format("stock : {0} year : {1} quarter {2}", stock, item.Key.Year, item.Key.Quarter));
                    lbStatus.SelectedIndex = lbStatus.Items.Count - 1;
                    string strWebContent = Tool.GetContentFromUrll(String.Format(address, item.Value, stock));

                    int start1 = 0;

                    for (int i = 0; i < flags.Length; i++)
                    {
                        foreach (Dictionary<int, int> gls in guideLines[i])
                        {
                            start1 = strWebContent.IndexOf(flags[i], start1 + 10);
                            if (start1 == -1)
                            {                                
                                break;
                            }
                            int end1 = strWebContent.IndexOf("</table>", start1);
                            string content1 = strWebContent.Substring(start1, end1 - start1 + 8);

                            htmldoc1.OpenNew(true);
                            htmldoc1.Write(content1);

                            foreach (KeyValuePair<int, int> gl in gls)
                            {
                                string str = htmldoc1.GetElementsByTagName("TR")[gl.Key].GetElementsByTagName("TD")[1].InnerText;
                                str = (str == null ? null : str.Trim());
                                decimal v = -77777.77m;
                                if (!string.IsNullOrEmpty(str))
                                {
                                    v = decimal.Parse(str);
                                }

                                DataTool.InsertGuideLine(stock, gl.Value, item.Key.Year, item.Key.Quarter, v);
                            }
                        }
                    }
                }                
            }

            MessageBox.Show("数据获取完成!");
            this.txtBeginQuarter.Enabled = true;
            this.txtBeginYear.Enabled = true;
            this.txtEndYear.Enabled = true;
            this.txtEndQuarter.Enabled = true;
            this.txtBeginStock.Enabled = true;
            this.txtEndStock.Enabled = true;
            this.cbReportType.Enabled = true;
            this.btnStartGetting.Enabled = true;
            this.btnStopGetting.Enabled = false;

            #region old code


            //string[] quarterStrings = new string[] { "03-15", "06-30", "09-30", "12-31" };

            //string[] stocks = DataTool.GetAllStocks(beginStock, endStock, StockType);

            //foreach (string stock in stocks)
            //{
            //    for (int year = beginYear; year <= endYear; year++)
            //    {
            //        for (int quarter = 1; quarter < 5; quarter++)
            //        {
            //            if (year == beginYear && quarter < beginQuarter)
            //            {
            //                quarter = beginQuarter;
            //            }

            //            if (year == endYear && quarter > endQuarter)
            //            {
            //                break;
            //            }

            //            lbStatus.Items.Add(String.Format("stock : {0} year : {1} quarter {2}", stock, year, quarter));
            //            lbStatus.SelectedIndex = lbStatus.Items.Count - 1;
            //            string strWebContent = Tool.GetContentFromUrll(String.Format(address, year, quarterStrings[quarter - 1], stock));

            //            int start1 = 0;

            //            for (int i = 0; i < flags.Length; i++)
            //            {
            //                foreach (Dictionary<int, int> gls in guideLines[i])
            //                {
            //                    start1 = strWebContent.IndexOf(flags[i], start1 + 10);
            //                    if (start1 == -1)
            //                    {
            //                        Log(String.Format("未找到股票{0}在{1}年{2}季的数据", stock, year, quarter));
            //                        break;
            //                    }
            //                    int end1 = strWebContent.IndexOf("</table>", start1);
            //                    string content1 = strWebContent.Substring(start1, end1 - start1 + 8);

            //                    htmldoc1.OpenNew(true);
            //                    htmldoc1.Write(content1);

            //                    foreach (KeyValuePair<int, int> gl in gls)
            //                    {
            //                        string str = htmldoc1.GetElementsByTagName("TR")[gl.Key].GetElementsByTagName("TD")[1].InnerText;
            //                        str = (str == null ? null : str.Trim());
            //                        decimal v = 0m;
            //                        if (!string.IsNullOrEmpty(str))
            //                        {
            //                            v = decimal.Parse(str);
            //                        }

            //                        DataTool.InsertGuideLine(stock, gl.Value, year, quarter, v);
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}

            //MessageBox.Show("数据获取完成!");
            //this.txtBeginQuarter.Enabled = true;
            //this.txtBeginYear.Enabled = true;
            //this.txtEndYear.Enabled = true;
            //this.txtEndQuarter.Enabled = true;
            //this.txtBeginStock.Enabled = true;
            //this.txtEndStock.Enabled = true;
            //this.cbReportType.Enabled = true;
            //this.btnStartGetting.Enabled = true;
            //this.btnStopGetting.Enabled = false;

            #endregion
        }

        

        

        private void Log(string message)
        {
            StreamWriter sw = new StreamWriter("log.txt", true);
            sw.WriteLine(message);
            sw.Close();
        }       

     

        private void btnDeleteGuideLines_Click(object sender, EventArgs e)
        {
            //for (int i = 1; i < 167; i++)
            //{
            //    DeleteGuideLines(this.txtDeleteStockNO.Text.Trim(), "t_guideLines_" + i.ToString());
            //}

            //for (int i = 2001; i < 2068; i++)
            //{
            //    DeleteGuideLines(this.txtDeleteStockNO.Text.Trim(), "t_guideLines_" + i.ToString());
            //}

            for (int i = 3001; i < 3194; i++)
            {
                DeleteGuideLines(this.txtDeleteStockNO.Text.Trim(), "t_guideLines_" + i.ToString());
            }
        }

        private void DeleteGuideLines(string stockNO, string tableName)
        {
            SqlConnection cn = new SqlConnection("SERVER=192.168.1.100;uid=sa;pwd=sa;database=JYStockAnalyser");
            SqlCommand cmd = new SqlCommand("DELETE " + tableName + " WHERE stock = '000584'", cn);
            // 
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        private void btnViewDataLocation_Click(object sender, EventArgs e)
        {
            new FrmViewDataLocation().ShowDialog();
        }

        private void btnCreateGuideLineTables_Click(object sender, EventArgs e)
        {
            new FrmCreateGuidelineTables().ShowDialog();
        }

        private void FrmGetGuideLines_Load(object sender, EventArgs e)
        {

        }
    }

    class YearQuarter
    {
        public YearQuarter(int year, int quarter)
        {
            Year = year;
            Quarter = quarter;
        }

        public int Year { get; set; }
        public int Quarter { get; set; }
    }
}
